package com.game.battleship.adapters;

import com.game.battleship.data.Game;
import com.game.battleship.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

/**
 * @author vtkhanh
 * 
 */
public class BattleBoardImageAdapter extends BaseAdapter {

	private static final int CELLS_COUNT = Game.BOARD_SIZE;
	private static final int IMG_WATER = R.drawable.transparent;
	private static final int IMG_SHIP  = R.drawable.ship;
	private static final int IMG_CRASH  = R.drawable.crash;
	private static final int IMG_MISS = R.drawable.miss;
	private static final int IMG_CROSSHAIR = R.drawable.crosshair;

	private Context mContext;
	
	private int mCellSize;
	private short[] mFormation = null; // show formation of human player
	private ImageView[] mBattleBoard = new ImageView[CELLS_COUNT];	// human player shots on this board
	
	/*private Integer[] mCellImages = { R.drawable.transparent, R.drawable.crash,
			R.drawable.miss, R.drawable.crosshair, R.drawable.ship };*/
	
	public BattleBoardImageAdapter(Context context, int cellSize) {
		mContext = context;
		mCellSize = cellSize;		
	}
	
	public BattleBoardImageAdapter(Context context, int cellSize, short[] formation){
		mContext = context;
		mCellSize = cellSize;
		mFormation = formation;
	}

	public int getCount() {
		return CELLS_COUNT;
	}

	public Object getItem(int position) {
		return mBattleBoard[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			imageView = new ImageView(mContext);			
			imageView.setLayoutParams(new GridView.LayoutParams(mCellSize, mCellSize));
			imageView.setScaleType(ScaleType.FIT_XY);			
			imageView.setPadding(0, 0, 0, 0);
		} else {
			imageView = (ImageView) convertView;
		}
		
		if ((mFormation != null) && (mFormation[position] != Game.WATER)) {
			imageView.setImageResource(getShip());
		}
		else {
			imageView.setImageResource(getWater());
		}

		if (mBattleBoard[position] == null) {
			mBattleBoard[position] = imageView;
		}
		
		return imageView;
	}
	
	/**
	 * Set crash image at position
	 * 
	 * @param	position:	place to set crash image
	 */
	public void setCrash(int position) {
		((ImageView) getItem(position)).setImageResource(getCrash());
	}
	
	/**
	 * Set miss image at position
	 * 
	 * @param	position:	place to set miss image
	 */
	public void setMiss(int position) {
		((ImageView) getItem(position)).setImageResource(getMiss());
	}	

	/** 
	 * @return the Integer id for transparent image
	 */
	public Integer getWater() {
		return IMG_WATER;
	}

	/**
	 * @return the Integer id for crash image
	 */
	public Integer getCrash() {
		return IMG_CRASH;
	}

	/** 
	 * @return the Integer id for miss image
	 */
	public Integer getMiss() {
		return IMG_MISS;
	}

	/** 
	 * @return the Integer id for crosair image
	 */
	public Integer getCrosshair() {
		return IMG_CROSSHAIR;
	}
	
	/** 
	 * @return the Integer id for miss image
	 */
	public Integer getShip() {
		return IMG_SHIP;
	}

}
